package adapletplugin.editors;

import objects.IObject;
import objects.ReferenceType;
import objects.Table;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.FormToolkit;

import sections.ISection.Section;
import util.ISectionListener;

public class RefTypeEditor extends AbstractSubEditor implements ISectionListener
{
	private Text fTypeText;
	private Combo fTableText;
	private Combo fIdText;
	private Combo fOidText;
	private Combo fNameText;
	private Combo fRevText;
	private Combo fVaultText;
	
	private ReferenceType focusRefType;

	public RefTypeEditor(ScrolledComposite composite, FormToolkit toolkit, MapFileEditor parentEditor)
	{
		super(composite, toolkit, parentEditor);
		fMap.registerListener(Section.TABLE, this);
	}

	@Override
	protected void create()
	{
		Composite row = fToolkit.createComposite(fSubComposite);
		row.setLayout(new GridLayout(2, false));
		row.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

		fToolkit.createLabel(row, "Type Name:");
		fTypeText = fToolkit.createText(row, "", SWT.BORDER | SWT.READ_ONLY);
		fTypeText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
//		fTypeText.addModifyListener(createModifyListener_Type());

		fToolkit.createLabel(row, "Table Name:");
		fTableText = new Combo(row, SWT.BORDER);
		fTableText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		fTableText.addModifyListener(createModifyListener_Table());

		fToolkit.createLabel(row, "ID Column:");
		fIdText = new Combo(row, SWT.BORDER);
		fIdText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		fIdText.addModifyListener(createModifyListener_ID());

		fToolkit.createLabel(row, "OID Column:");
		fOidText = new Combo(row, SWT.BORDER);
		fOidText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		fOidText.addModifyListener(createModifyListener_OID());

		fToolkit.createLabel(row, "Name Column:");
		fNameText = new Combo(row, SWT.BORDER);
		fNameText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		fNameText.addModifyListener(createModifyListener_Name());

		fToolkit.createLabel(row, "Revision Column:");
		fRevText = new Combo(row, SWT.BORDER);
		fRevText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		fRevText.addModifyListener(createModifyListener_Rev());

		fToolkit.createLabel(row, "Vault:");
		fVaultText = new Combo(row, SWT.BORDER);
		fVaultText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		fVaultText.addModifyListener(createModifyListener_Vault());

	}

//	private ModifyListener createModifyListener_Type()
//	{
//		return new ModifyListener() {
//
//			@Override
//			public void modifyText(ModifyEvent e)
//			{
//				if (focusRefType != null)
//				{
//					focusRefType.setTypeName(fTypeText.getText());
//					fEditor.refresh();
//				}
//			}
//		};
//	}

	private ModifyListener createModifyListener_Table()
	{
		return new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e)
			{
				if (focusRefType != null)
				{
					focusRefType.setTableName(fTableText.getText());
					populateColumnComboChoices();
					fEditor.refresh();
				}
			}
		};
	}

	private ModifyListener createModifyListener_ID()
	{
		return new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e)
			{
				if (focusRefType != null)
				{
					focusRefType.setId(fIdText.getText());
					fEditor.refresh();
				}
			}
		};
	}

	private ModifyListener createModifyListener_OID()
	{
		return new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e)
			{
				if (focusRefType != null)
				{
					focusRefType.setOid(fOidText.getText());
					fEditor.refresh();
				}
			}
		};
	}

	private ModifyListener createModifyListener_Name()
	{
		return new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e)
			{
				if (focusRefType != null)
				{
					focusRefType.setName(fNameText.getText());
					fEditor.refresh();
				}
			}
		};
	}

	private ModifyListener createModifyListener_Rev()
	{
		return new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e)
			{
				if (focusRefType != null)
				{
					focusRefType.setRevision(fRevText.getText());
					fEditor.refresh();
				}
			}
		};
	}

	private ModifyListener createModifyListener_Vault()
	{
		return new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e)
			{
				if (focusRefType != null)
				{
					focusRefType.setVault(fVaultText.getText());
					fEditor.refresh();
				}
			}
		};
	}

	@Override
	protected void updateFields()
	{
		focusRefType = (ReferenceType) fFocusObject;
		if(focusRefType == null)
		{
			fTypeText.setText("");
			fTableText.setText("");
			fIdText.setText("");
			fOidText.setText("");
			fNameText.setText("");
			fRevText.setText("");
			fVaultText.setText("");
		}
		else
		{
			fTypeText.setText(focusRefType.getTypeName());
			fTableText.setText(focusRefType.getTableName());
			fIdText.setText(focusRefType.getId());
			fOidText.setText(focusRefType.getOid());
			fNameText.setText(focusRefType.getName());
			fRevText.setText(focusRefType.getRevision());
			fVaultText.setText(focusRefType.getVault());
		}
		
		populateTableComboChoices();
		populateColumnComboChoices();
	}

	@Override
	public void acceptChange(Section changedSection)
	{
		switch(changedSection)
		{
		case TABLE:
			populateTableComboChoices();
			populateColumnComboChoices();
			break;
		default:
			break;
		}
	}
	
	@Override
	protected void populateTableComboChoices()
	{
		IObject[] tables = fMap.getTableSection().getObjects();
		
		String currentText = fTableText.getText();
		
		fTableText.removeAll();
		
		for(IObject table : tables)
		{
			fTableText.add(table.getObjectName());
		}
		
		fTableText.setText(currentText);
	}

	@Override
	protected void populateColumnComboChoices()
	{
		String tableName = fTableText.getText();
		if(tableName == null || tableName.isEmpty())
		{
			setColumnNames(new String[0]);
			return;
		}
		
		Table table = fMap.getTableSection().getTable(tableName);
		if(table == null)
		{
			setColumnNames(new String[0]);
			return;
		}
		
		String[] columnNames = table.getColumnNames();
		
		setColumnNames(columnNames);
	}

	@Override
	protected void setColumnNames(String[] columnNames)
	{
		updateComboSelection(fIdText, columnNames, focusRefType == null ? "" : focusRefType.getId());
		updateComboSelection(fOidText, columnNames, focusRefType == null ? "" : focusRefType.getOid());
		updateComboSelection(fNameText, columnNames, focusRefType == null ? "" : focusRefType.getName());
		updateComboSelection(fRevText, columnNames, focusRefType == null ? "" : focusRefType.getRevision());
		updateComboSelection(fVaultText, columnNames, focusRefType == null ? "" : focusRefType.getVault());
	}

}
